package com.retail.xiaocui.server.service.impl;

import java.util.List;

import com.retail.common.core.domain.Result;
import com.retail.common.security.utils.SecurityUtils;
import com.retail.xiaocui.common.domain.req.OrderReq;
import com.retail.xiaocui.server.look.DIstributedLockClient;
import com.retail.xiaocui.server.look.RedisLuaLook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.retail.xiaocui.server.mapper.WarehouseSkuMapper;
import com.retail.xiaocui.common.domain.WarehouseSku;
import com.retail.xiaocui.server.service.WarehouseSkuService;

/**
 * 商品库存Service业务层处理
 *
 * @author retail
 * @date 2024-03-10
 */
@Service
public class WarehouseSkuServiceImpl implements WarehouseSkuService
{
    @Autowired
    private WarehouseSkuMapper warehouseSkuMapper;


    @Autowired
    private DIstributedLockClient dIstributedLockClient;


    /**
     * 查询商品库存
     *
     * @param warehouseSkuId 商品库存主键
     * @return 商品库存
     */
    @Override
    public WarehouseSku selectWarehouseSkuByWarehouseSkuId(Integer warehouseSkuId)
    {
        return warehouseSkuMapper.selectWarehouseSkuByWarehouseSkuId(warehouseSkuId);
    }

    /**
     * 查询商品库存列表
     *
     * @param warehouseSku 商品库存
     * @return 商品库存
     */
    @Override
    public List<WarehouseSku> selectWarehouseSkuList(WarehouseSku warehouseSku)
    {
        return warehouseSkuMapper.selectWarehouseSkuList(warehouseSku);
    }

    /**
     * 新增商品库存
     *
     * @param warehouseSku 商品库存
     * @return 结果
     */
    @Override
    public int insertWarehouseSku(WarehouseSku warehouseSku)
    {
        return warehouseSkuMapper.insertWarehouseSku(warehouseSku);
    }

    /**
     * 修改商品库存
     *
     * @param warehouseSku 商品库存
     * @return 结果
     */
    @Override
    public int updateWarehouseSku(WarehouseSku warehouseSku)
    {
        return warehouseSkuMapper.updateWarehouseSku(warehouseSku);
    }

    /**
     * 批量删除商品库存
     *
     * @param warehouseSkuIds 需要删除的商品库存主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseSkuByWarehouseSkuIds(Integer[] warehouseSkuIds)
    {
        return warehouseSkuMapper.deleteWarehouseSkuByWarehouseSkuIds(warehouseSkuIds);
    }

    /**
     * 删除商品库存信息
     *
     * @param warehouseSkuId 商品库存主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseSkuByWarehouseSkuId(Integer warehouseSkuId)
    {
        return warehouseSkuMapper.deleteWarehouseSkuByWarehouseSkuId(warehouseSkuId);
    }



    private static  int count=0;
    @Override
    public Result orderRequest(OrderReq orderReq) {

        //获取到当前登录人id
        Long userId = SecurityUtils.getUserId();
        RedisLuaLook redisLuaLock = dIstributedLockClient.getRedisLuaLock("Lock" + userId);
        redisLuaLock.lock();
        try {
            //根据id去查询相应的对象
            WarehouseSku warehouseSku = warehouseSkuMapper.selectWarehouseSkuBySkuId(orderReq.getSkuId());
            //获取到该对象的库存数量
            Integer stashCount = warehouseSku.getStashCount();
            myMethod();
            //进行判断
            if(orderReq.getCount()<stashCount){
             warehouseSkuMapper.updateWarehouseStashCount(orderReq);
                return Result.success(200,"下单成功");
            }else {
                return Result.error("没有库存了");
            }
        }finally {
            redisLuaLock.unlock();
            System.out.println("使用了"+count);
        }
    }

    public static  void myMethod(){
        count++;
    }

}
